package com.ordertracker.service;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ordertracker.Application;
import com.ordertracker.Session;
import com.ordertracker.dao.GeneralCrudDao;
import com.ordertracker.exception.ValidationException;
import com.ordertracker.persistent.constant.DictionaryState;
import com.ordertracker.persistent.dictionary.Client;
import com.ordertracker.persistent.dictionary.Currency;
import com.ordertracker.persistent.dictionary.Organization;
import com.ordertracker.persistent.document.PaymentOrder;
import com.ordertracker.protocol.CommandMessage;
import com.ordertracker.to.TransferObjectFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

/**
 * Description of class.
 * Created 2/12/13 4:50 PM
 *
 * @author andrey.rodin@playtech.com
 */

@Service(value = "paymentorder")
public class PaymentOrderManager extends AbstractCRUDEventHandle<PaymentOrder> {

    private GeneralCrudDao dao;

    @Resource(name = "crudDao")
    public void setDao(GeneralCrudDao<Client, Long> dao) {
        this.dao = dao;
    }
	
	
	@Override
	protected PaymentOrder unmarshal(String obj) throws Exception {
        StringReader sr = new StringReader(obj);
        System.out.println("Position manager: create: "+sr);
        com.ordertracker.entity.document.PaymentOrder paymentOrder = Application.getApplication().getJsonUnmarshaller().unmarshalFromJSON(sr,
                    com.ordertracker.entity.document.PaymentOrder.class);

        return transferObjectFactory.paymentOrderFromDTO(paymentOrder);
	}

	@Override
	protected String getServiceName() {
		return "paymentorder";
	}

	@Override
	protected void validate(Session session, PaymentOrder msg, ActionType type)
			throws ValidationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected Response otherAction(Session session, CommandMessage msg,
			PaymentOrder obj, ActionType type) {
		
    	switch (type){
    	case GET_ALL_LIST:
    		HashMap<Object, Object> map = new HashMap<Object, Object>();
    		map.put("state", DictionaryState.ACTIVE);
    		map.put("organization", session.getClient().getOrganization());
    		map.put("client", session.getClient());
    		
    		List<PaymentOrder> list = dao.getAllByCriteria(map, PaymentOrder.class);
    		
    		List<com.ordertracker.entity.document.PaymentOrder> poList = new ArrayList<com.ordertracker.entity.document.PaymentOrder>();

    		for (PaymentOrder po : list){
    			poList.add(transferObjectFactory.paymentOrderToDTO(po));
    		}
    		
    		return Response.ok(poList).build();
		default:
			return null;
    	}
	}

	@Override
	protected boolean needToUnmarshal(ActionType type) {
		switch (type) {
		case ADD:
		case UPDATE:
			return true;
		default:
			return false;
		}
	}

	@Override
	@Transactional
	protected com.ordertracker.entity.document.PaymentOrder create(Session session, PaymentOrder obj) {
		Organization org = (Organization) dao.findById(obj.getOrganization().getId(), Organization.class);
		Client client = (Client) dao.findById(obj.getClient().getId(), Client.class);
		Currency currency = (Currency) dao.findEqualByCriteria("code", obj.getCurrency().getCode(), Currency.class);
		
		if (currency == null){
			dao.saveEntity(obj.getCurrency());
			currency = (Currency) dao.findEqualByCriteria("code", obj.getCurrency().getCode(), Currency.class);
		}
		
		obj.setOrganization(org);
		obj.setClient(client);
		obj.setAudit(transferObjectFactory.generateAuditInfo(session, false));
		obj.setCurrency(currency);
		
		PaymentOrder po = (PaymentOrder) dao.saveEntity(obj);
		
		return transferObjectFactory.paymentOrderToDTO(po);
	}

	@Override
	@Transactional(readOnly = true)
	protected Object receive(CommandMessage msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional
	protected Object delete(Session session, PaymentOrder obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional
	protected Object update(Session session, PaymentOrder obj) {
		// TODO Auto-generated method stub
		return null;
	}


}
